import { createAction, actionTypeCreatorWithNameSpace } from './util';
import agent from 'agent';

const actionTypeCreator = actionTypeCreatorWithNameSpace('machine');

export const search = createAction(actionTypeCreator('search'), (query) => {
  return agent.get('/api/machines/search')
    .query(query)
    .then(response => response.body);
});

export const create = createAction(actionTypeCreator('create'), (machine) => {
  return agent.post('/api/machines')
    .send(machine)
    .then(() => machine);
});

export const initialize = createAction(actionTypeCreator('initialize'), (machine) => {
  return agent.post(`/api/machines/${machine.id}/initialize`)
    .then(() => machine);
});

export const provide = createAction(actionTypeCreator('provide'), (machine) => {
  return agent.put(`/api/machines/${machine.id}/provide`)
    .then(() => machine);
});

export const log = createAction(actionTypeCreator('log'), (machine, ns) => {
  return agent.get(`/api/machines/${machine.id}/log?namespace=${ns}`)
    .then(response => response.body);
});

export const remove = createAction(actionTypeCreator('remove'), (machine) => {
  return agent.del(`/api/machines/${machine.id}`)
    .then(() => machine);
});

export const assign = createAction(actionTypeCreator('assign'), (machineId, companyId) => {
  return agent.put(`/api/machines/${machineId}/assign?companyId=${companyId}`)
    .then(true);
});

export const listAllMachineSources = createAction(actionTypeCreator('listAllMachineSources'), () => {
  return agent.get('/api/machine-sources/all')
    .then(response => response.body);
});

export const createMachineSource = createAction(actionTypeCreator('createMachineSource'), (source) => {
  return agent.post('/api/machine-sources')
    .send(source)
    .then(true);
});

export const updateMachineSource = createAction(actionTypeCreator('updateMachineSource'), (source) => {
  return agent.post(`/api/machine-sources/${source.id}/update`)
    .send(source)
    .then(true);
});

export const lockMachineSource = createAction(actionTypeCreator('lockMachineSource'), (source) => {
  return agent.post(`/api/machine-sources/${source.id}/lock`)
    .then(true);
});

export const unlockMachineSource = createAction(actionTypeCreator('unlockMachineSource'), (source) => {
  return agent.post(`/api/machine-sources/${source.id}/unlock`)
    .then(true);
});


export const searchMachineByCompanyId = createAction(actionTypeCreator('search'), (query, companyId) => {
  return agent.get(`/api/companies/${companyId}/machines/search`)
    .query(query)
    .then(response => response.body);
});

export const createCompanyMachine = createAction(actionTypeCreator('createCompanyMachine'), (machine, companyId) => {
  return agent.post(`/api/companies/${companyId}/machines`)
    .send(machine)
    .then(() => machine);
});

export const listMachineSourcesByCompanyId = createAction(actionTypeCreator('listCompanyMachineSources'), (companyId) => {
  return agent.get(`/api/companies/${companyId}/machine-sources`)
    .then(response => response.body);
});

export const createCompanyMachineSource = createAction(actionTypeCreator('createCompanyMachineSource'), (companyId, source) => {
  return agent.post(`/api/companies/${companyId}/machine-sources`)
    .send(source)
    .then(true);
});

export const updateCompanyMachineSource = createAction(actionTypeCreator('updateCompanyMachineSource'), (companyId, source) => {
  return agent.put(`/api/companies/${companyId}/machine-sources/${source.id}`)
    .send(source)
    .then(true);
});

export const cleanMachineList = createAction(actionTypeCreator('cleanMachineList'), () => {
  return {};
});

export const updateMachineStatus = createAction(actionTypeCreator('updateMachineStatus'));
